package br.gendi.dialog;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import au.id.jericho.lib.html.Source;
import au.id.jericho.lib.html.SourceFormatter;
import au.id.jericho.lib.html.Tag;

public class Visualizador {

	private Shell sShell = null;
	private TabFolder tabFolder = null;

	public Visualizador() {
		super();
		createSShell();
		//sShell.open();
	}

	private void createSShell() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.horizontalSpacing = 2;
		gridLayout.marginWidth = 2;
		gridLayout.marginHeight = 2;
		gridLayout.verticalSpacing = 2;
		sShell = new Shell();		   
		sShell.setText("Resultados");
		createTabFolder();
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(800,600));

	}

	private void createTabFolder() {
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		tabFolder = new TabFolder(sShell, SWT.NONE);
		tabFolder.setLayoutData(gridData);
	}


	private boolean done = false;
	public void adicionarSite(final String nome, String url) {
		final Browser b = new Browser(tabFolder, SWT.NONE);
		b.setUrl(url);
		
		
		TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE);
		tabItem1.setControl(b);
		tabItem1.setText(nome);

		b.addStatusTextListener(new StatusTextListener() {
			public void changed(StatusTextEvent arg0) {
				if (arg0.text.equalsIgnoreCase("Concluído") || arg0.text.equalsIgnoreCase("done")) {
					if (done == false) {
						String texto = ((Browser)arg0.widget).getText();
						done = true;
						salvarEmHTML(texto, nome+".html");
						Source source = null;
						try {
							source=new Source(new FileReader(new File(nome+".html")));
						} catch (Exception e) {
							e.printStackTrace();
						}
						
						List l = source.findAllElements(Tag.IMG);
						String html = source.toString();
						for (int i=0;i<l.size(); i++) 
							html = html.replaceAll(l.get(i).toString(), "");
						
						b.setText(html);
					}
					if (!sShell.isVisible())
						sShell.open();
				}
			}
		});
		
	}

	public void adicionarSiteHTML(String nome, String html) {
		Browser b = new Browser(tabFolder, SWT.NONE);
		salvarEmHTML(html, nome+".html");
		
		Source source = null;
		try {
			source=new Source(new FileReader(new File(nome+".html")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List l = source.findAllElements(Tag.IMG);
		String html2 = source.toString();
		for (int i=0;i<l.size(); i++) 
			html2 = html2.replaceAll(l.get(i).toString(), "");
		
		b.setText(html2);
		
		TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE);
		tabItem1.setControl(b);
		tabItem1.setText(nome);
		
		b.addStatusTextListener(new StatusTextListener() {
			public void changed(StatusTextEvent arg0) {
				if (!sShell.isVisible())
					sShell.open();
			}
		});

	}
	

	private void salvarEmHTML(String texto, String fileName) {

		try
		{
			//File f = new File(".");
			//System.out.println(f.getAbsolutePath());
			FileWriter fileWriter = new FileWriter( fileName );
			BufferedWriter bufferedWriter = new BufferedWriter( fileWriter );
			bufferedWriter.write(texto);
			bufferedWriter.close();
		}
		catch( IOException e )
		{
			e.printStackTrace();
		}
	}
}
